package com.chenqq.camerademo.camera.es;


import android.util.Log;

import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.LinkedBlockingQueue;


public abstract class MessageTask
        implements Runnable {
    private static final String TAG = MessageTask.class.getSimpleName();
    protected static final int REQUEST_TASK_NON = 0;
    protected static final int REQUEST_TASK_RUN = -1;
    protected static final int REQUEST_TASK_RUN_AND_WAIT = -2;
    protected static final int REQUEST_TASK_START = -8;
    protected static final int REQUEST_TASK_QUIT = -9;

    public static class TaskBreak extends RuntimeException {
    }

    protected static final class Request {
        int request;
        int arg1;
        int arg2;

        private Request() {
            this.request = this.request_for_result = 0;
        }


        Object obj;

        int request_for_result;

        Object result;

        public Request(int _request, int _arg1, int _arg2, Object _obj) {
            this.request = _request;
            this.arg1 = _arg1;
            this.arg2 = _arg2;
            this.obj = _obj;
            this.request_for_result = 0;
        }

        public void setResult(Object result) {
            synchronized (this) {
                this.result = result;
                this.request = this.request_for_result = 0;
                notifyAll();
            }
        }


        public boolean equals(Object o) {
            return (o instanceof Request) ? ((this.request == ((Request) o).request && this.request_for_result == ((Request) o).request_for_result && this.arg1 == ((Request) o).arg1 && this.arg2 == ((Request) o).arg2 && this.obj == ((Request) o).obj)) : super
                    .equals(o);
        }
    }


    private final Object mSync = new Object();

    private final int mMaxRequest;

    private final LinkedBlockingQueue<Request> mRequestPool;

    private final LinkedBlockingDeque<Request> mRequestQueue;

    private volatile boolean mIsRunning;

    private volatile boolean mFinished;
    private Thread mWorkerThread;

    public MessageTask() {
        this.mMaxRequest = -1;
        this.mRequestPool = new LinkedBlockingQueue<>();
        this.mRequestQueue = new LinkedBlockingDeque<>();
    }


    public MessageTask(int init_num) {
        this.mMaxRequest = -1;
        this.mRequestPool = new LinkedBlockingQueue<>();
        this.mRequestQueue = new LinkedBlockingDeque<>();
        for (int i = 0; i < init_num &&
                this.mRequestPool.offer(new Request()); i++)
            ;
    }


    public MessageTask(int max_request, int init_num) {
        this.mMaxRequest = max_request;
        this.mRequestPool = new LinkedBlockingQueue<>(max_request);
        this.mRequestQueue = new LinkedBlockingDeque<>(max_request);
        for (int i = 0; i < init_num &&
                this.mRequestPool.offer(new Request()); i++)
            ;
    }


    protected void init(int arg1, int arg2, Object obj) {
        this.mFinished = false;
        this.mRequestQueue.offer(obtain(-8, arg1, arg2, obj));
    }


    protected abstract void onInit(int paramInt1, int paramInt2, Object paramObject);


    protected abstract void onStart();


    protected void onBeforeStop() {
    }


    protected abstract void onStop();


    protected abstract void onRelease();


    protected boolean onError(Exception e) {
        return true;
    }


    protected abstract Object processRequest(int paramInt1, int paramInt2, int paramInt3, Object paramObject) throws TaskBreak;


    protected Request takeRequest() throws InterruptedException {
        return this.mRequestQueue.take();
    }


    public boolean waitReady() {
        synchronized (this.mSync) {
            while (!this.mIsRunning && !this.mFinished) {
                try {
                    this.mSync.wait(500L);
                } catch (InterruptedException e) {
                    break;
                }
            }
            return this.mIsRunning;
        }
    }


    public boolean isRunning() {
        return this.mIsRunning;
    }


    public boolean isFinished() {
        return this.mFinished;
    }


    public void run() {
        Request request = null;
        this.mIsRunning = true;
        try {
            request = this.mRequestQueue.take();
        } catch (InterruptedException e) {
            this.mIsRunning = false;
            this.mFinished = true;
        }
        synchronized (this.mSync) {
            if (this.mIsRunning) {
                this.mWorkerThread = Thread.currentThread();
                try {
                    onInit(request.arg1, request.arg2, request.obj);
                } catch (Exception e) {
                    Log.w(TAG, e);
                    this.mIsRunning = false;
                    this.mFinished = true;
                }
            }
            this.mSync.notifyAll();
        }
        if (this.mIsRunning) {
            try {
                onStart();
            } catch (Exception e) {
                if (callOnError(e)) {
                    this.mIsRunning = false;
                    this.mFinished = true;
                }
            }
        }
        while (this.mIsRunning) {
            try {
                request = takeRequest();
                switch (request.request) {
                    case 0:
                        break;
                    case -9:
                        break;
                    case -1:
                        if (request.obj instanceof Runnable) {
                            try {
                                ((Runnable) request.obj).run();
                            } catch (Exception e) {
                            }
                        }
                        break;


                    case -2:
                        try {
                            request.setResult(processRequest(request.request_for_result, request.arg1, request.arg2, request.obj));
                        } catch (TaskBreak e) {
                            request.setResult(null);
                            break;
                        } catch (Exception e) {
                            request.setResult(null);
                        }
                        break;


                    default:
                        try {
                            processRequest(request.request, request.arg1, request.arg2, request.obj);
                        } catch (TaskBreak e) {
                            break;
                        } catch (Exception e) {
                        }
                        break;
                }


                request.request = request.request_for_result = 0;

                this.mRequestPool.offer(request);
            } catch (InterruptedException e) {
                break;
            }
        }
        boolean interrupted = Thread.interrupted();
        synchronized (this.mSync) {
            this.mWorkerThread = null;
            this.mIsRunning = false;
            this.mFinished = true;
        }
        if (!interrupted) {
            try {
                onBeforeStop();
                onStop();
            } catch (Exception e) {
                callOnError(e);
            }
        }
        try {
            onRelease();
        } catch (Exception exception) {
        }


        synchronized (this.mSync) {
            this.mSync.notifyAll();
        }
    }


    protected boolean callOnError(Exception e) {
        try {
            return onError(e);
        } catch (Exception exception) {


            return true;
        }
    }


    protected Request obtain(int request, int arg1, int arg2, Object obj) {
        Request req = this.mRequestPool.poll();
        if (req != null) {
            req.request = request;
            req.arg1 = arg1;
            req.arg2 = arg2;
            req.obj = obj;
        } else {
            req = new Request(request, arg1, arg2, obj);
        }
        return req;
    }


    public boolean offer(int request, int arg1, int arg2, Object obj) {
        return (!this.mFinished && this.mRequestQueue.offer(obtain(request, arg1, arg2, obj)));
    }


    public boolean offer(int request, int arg1, Object obj) {
        return (!this.mFinished && this.mRequestQueue.offer(obtain(request, arg1, 0, obj)));
    }


    public boolean offer(int request, int arg1, int arg2) {
        return (!this.mFinished && this.mIsRunning && this.mRequestQueue.offer(obtain(request, arg1, arg2, null)));
    }


    public boolean offer(int request, int arg1) {
        return (!this.mFinished && this.mIsRunning && this.mRequestQueue.offer(obtain(request, arg1, 0, null)));
    }


    public boolean offer(int request) {
        return (!this.mFinished && this.mIsRunning && this.mRequestQueue.offer(obtain(request, 0, 0, null)));
    }


    public boolean offer(int request, Object obj) {
        return (!this.mFinished && this.mIsRunning && this.mRequestQueue.offer(obtain(request, 0, 0, obj)));
    }


    public boolean offerFirst(int request, int arg1, int arg2, Object obj) {
        return (!this.mFinished && this.mIsRunning && this.mRequestQueue.offerFirst(obtain(request, arg1, arg2, obj)));
    }


    public Object offerAndWait(int request, int arg1, int arg2, Object obj) {
        if (!this.mFinished && request > 0) {
            Request req = obtain(-2, arg1, arg2, obj);
            synchronized (req) {
                req.request_for_result = request;
                req.result = null;
                this.mRequestQueue.offer(req);
                while (this.mIsRunning && req.request_for_result != 0) {
                    try {
                        req.wait(100L);
                    } catch (InterruptedException e) {
                        break;
                    }
                }
            }
            return req.result;
        }
        return null;
    }


    public boolean queueEvent(Runnable task) {
        return (!this.mFinished && task != null && offer(-1, task));
    }


    public void removeRequest(Request request) {
        for (Request req : this.mRequestQueue) {
            if (!this.mIsRunning || this.mFinished)
                break;
            if (req.equals(request)) {
                this.mRequestQueue.remove(req);
                this.mRequestPool.offer(req);
            }
        }
    }

    public void removeRequest(int request) {
        for (Request req : this.mRequestQueue) {
            if (!this.mIsRunning || this.mFinished)
                break;
            if (req.request == request) {
                this.mRequestQueue.remove(req);
                this.mRequestPool.offer(req);
            }
        }
    }


    public void release() {
        release(false);
    }


    public void release(boolean interrupt) {
        boolean b = this.mIsRunning;
        this.mIsRunning = false;
        if (!this.mFinished) {
            this.mRequestQueue.clear();
            this.mRequestQueue.offerFirst(obtain(-9, 0, 0, null));
            synchronized (this.mSync) {
                if (b) {
                    long current = Thread.currentThread().getId();
                    long id = (this.mWorkerThread != null) ? this.mWorkerThread.getId() : current;
                    if (id != current) {
                        if (interrupt && this.mWorkerThread != null) {
                            this.mWorkerThread.interrupt();
                        }
                        while (!this.mFinished) {
                            try {
                                this.mSync.wait(300L);
                            } catch (InterruptedException interruptedException) {
                            }
                        }
                    }
                }
            }
        }
    }


    public void releaseSelf() {
        this.mIsRunning = false;
        if (!this.mFinished) {
            this.mRequestQueue.clear();
            this.mRequestQueue.offerFirst(obtain(-9, 0, 0, null));
        }
    }


    public void userBreak() throws TaskBreak {
        throw new TaskBreak();
    }
}


/* Location:              E:\tools\工具\classes.jar!\com\serenegian\\utils\MessageTask.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.1.2
 */